/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wso.controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.HttpRequestHandler;
import wso.bus.UserBus;
import wso.dto.WsoUser;
import wso.global.Constants;

/**
 *
 * @author Violet_Ink
 */
public class LoginController implements HttpRequestHandler {

    private UserBus userBus;

    public void setUserBus(UserBus userBus) {
        this.userBus = userBus;
    }

    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String action = request.getParameter("action");
        HttpSession session = request.getSession();
        String url = "dashboard.jsp";
        WsoUser user = null;

        if (action != null) {
            if (action.equals(Constants.ACTION_SIGIN)) {
                String username = request.getParameter("username");
                String password = request.getParameter("password");

                user = userBus.checkLogin(username, password);
                if (user != null) {
                    session.setAttribute(Constants.SESSION_USER, user);
                } else {
                    url = "login.jsp";
                }
            }
        }
        RequestDispatcher rd = request.getRequestDispatcher(url);
        rd.forward(request, response);
    }
}
